﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WordsGroup.Question;


namespace WordsGroup.Answer
{
    public class Jimson : IGroupWords
    {
        public int GetMaxGroupValue(string str)
        {
            string[] words = str.Split(new Char[] { ' ' });
            var a = new MyData[words.Length, words.Length];
            for (int i = 0; i < words.Length; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    a[i, j].Samestring = LCS(words[i], words[j]);
                    a[i, j].length = a[i, j].Samestring.Length;
                }
            }

            for (int i = 0; i < words.Length; i++)
            {
                for (int j = i; j < words.Length; j++)
                {

                }
            }

            return 6;
        }


        public string LCS(string str1, string str2)
        {

            if (str1 == str2)
            {
                return str1;
            }
            else if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2))
            {
                return null;
            }

            var d = new int[str1.Length, str2.Length];
            var index = 0;
            var length = 0;
            for (int i = 0; i < str1.Length; i++)
            {
                for (int j = 0; j < str2.Length; j++)
                {
                    var n = i - 1 >= 0 && j - 1 >= 0 ? d[i - 1, j - 1] : 0;

                    d[i, j] = str1[i] == str2[j] ? 1 + n : 0;

                    if (d[i, j] > length)
                    {
                        length = d[i, j];
                        index = i;
                    }
                }
            }

            return str1.Substring(index - length + 1, length);
        }

        struct MyData
        {
            public string Samestring;
            public int length;
        }



    }
}

